﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Web;
using System.Security.Cryptography;
namespace Bussiness {
    public class Helper {
        public static bool isImageAllow(String fileName) {
            String ext = Path.GetExtension(fileName);
            if (ext.Equals(".png") || ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".gif") || ext.Equals(".bmp"))
                return true;
            else
                return false;
        }

        public static String convertToSlug(String text){            
            Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = text.Normalize(NormalizationForm.FormD);
            return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(' ', '-').Trim();
        }

        public void createCategoryTree(ref DataTable categories, int start, int level, ref String catTree) {
            bool hasChild = false;
            foreach (DataRow child in categories.Rows) {
                if (child["ParentCatID"].ToString() == start.ToString()) {

                    if (hasChild == false) {
                        hasChild = true;

                        catTree += "<ul>";                        
                    }
                    catTree += "<li><a href=\"/Category.aspx?cat=" + child["CatID"] + "\">" + child["CatName"] + "</a>";
                    
                    //Response.Write(new String('-', level) + child["CatName"].ToString() + "<br/>");
                    createCategoryTree(ref categories, int.Parse(child["CatID"].ToString()), level + 1, ref catTree);

                    catTree += "</li>";
                }
            }

            if (hasChild) catTree += "</ul>";
        }


        public string Encrypt(string originalPassword) {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }
    }
}
